--File ： ChapterView.lua
--Author : zee
--Date   : 2015/4/8
--Description : 关卡主界面 chapter.fla 第一帧

-- require "tagMap/Tag_chapter"




RootLayer = class("RootLayer",function()
    -- 继承
    local ret = cc.Layer:create()

    return ret
end)

RootLayer.__index = RootLayer
RootLayer.__instance = nil

function RootLayer:create()
    local ret = RootLayer:new()
    RootLayer.__instance = ret
    return ret
end











--------------------------------------------------------
-- 初始化
--------------------------------------------------------
function RootLayer:start()
    require "BattleLayer/BattleLayer"
    require "RootLayer/GameDefine"

    -- 直接启动主战斗界面。
    local gameLayer = BattleLayer:create()  -- GameLayer:create()
    gameLayer:start()
    gameLayer:setLocalZOrder( 10 )
    self:addChild(gameLayer)

    -- self:testTouchLayer() -- ccw的控件组不能用，缺少这些个事件传递。
end








TouchLayer = class("TouchLayer",function()
    -- 继承
    local ret = cc.Layer:create()
    ret.res = true
    ret.say = "莫扔的"

    return ret
end)

function TouchLayer:create( b )
    local ret = TouchLayer:new()
    -- 手势
    b = b or false
    local function onTouchBegan( touch, event )
        cclook(" 你他妈摸我了: (%s)", ret.say )
        return ret.res
    end
    local function onTouchMoved( touch, event )
        cclook(" 我在移动 (%s)", ret.say )
        return ret.res
    end
    local function onTouchEnded( touch, event )
        cclook("结束了  (%s)",ret.say)
        return ret.res
    end
    ----
    local listener = cc.EventListenerTouchOneByOne:create()
    listener:setSwallowTouches( true )
    listener:registerScriptHandler( onTouchBegan , cc.Handler.EVENT_TOUCH_BEGAN )  
    listener:registerScriptHandler( onTouchMoved , cc.Handler.EVENT_TOUCH_MOVED )
    listener:registerScriptHandler( onTouchEnded , cc.Handler.EVENT_TOUCH_ENDED ) 
    ret:getEventDispatcher():addEventListenerWithSceneGraphPriority( listener, ret )
    ret.listener = listener
    return ret
end

function TouchLayer:reset(b)
    -- 手势
    local function onTouchBegan( touch, event )
        cclook(" 你他妈摸我了: (%s)", self.say )
        return self.res
    end
    local function onTouchMoved( touch, event )
        cclook(" 我在移动 (%s)", self.say )
        return  false
    end
    local function onTouchEnded( touch, event )
        cclook("结束了  (%s)",self.say)
        return false
    end
    ---- 移除上一个
    if self.listener then 
        self:getEventDispatcher():removeEventListener(self.listener)
    end
    ----
    local listener = cc.EventListenerTouchOneByOne:create()
    listener:setSwallowTouches( b )
    listener:registerScriptHandler( onTouchBegan , cc.Handler.EVENT_TOUCH_BEGAN )  
    listener:registerScriptHandler( onTouchMoved , cc.Handler.EVENT_TOUCH_MOVED )
    listener:registerScriptHandler( onTouchEnded , cc.Handler.EVENT_TOUCH_ENDED ) 
    self:getEventDispatcher():addEventListenerWithSceneGraphPriority( listener, self )
    self.listener = listener
end




function RootLayer:testTouchLayer()
    

    local b = TouchLayer:create(true )
    b.say = "我是最上面的b"
    

    local c = TouchLayer:create( true )
    c.say = "我是 b 下面的c"
    b:addChild(c)

    local d = TouchLayer:create( true )
    d.say = "我是最 c 下面的d"
    c:addChild(d)

    local a = TouchLayer:create( )
    a.say = "我是最下面的a"
    self:addChild(a)
    a:setLocalZOrder(49)


    self:addChild(b)
    b:setLocalZOrder(50)


    a:reset( false )
    b:reset( false )
    c:reset( true )
    d:reset( false )
    -- c:reset( true )
-- 结论 --
--[[
    a--  
    b-c-d
    
    1. 消息的传递和兄弟层的 zoder 层级有关系，越高的越先。
    2. 消息的传递的吞噬会全层发生，不论父子还是兄弟层。
    3. touch的吞噬，必须要该层在 touchBeagn方法返回true，否则，吞噬失效。
--]] 
end













--[[       试一试 gv 能不能在这上面用，结果是不行的。 
EquipViewCell = class("EquipViewCell",function()
    local ret = CGridViewCell:new()
    return ret
end)
EquipViewCell.__index       = EquipViewCell


function RootLayer:addGV( )
    cclook("你麻痹 --%s", 5)
    self.gv_equip = CGridView:create( cc.size(400,400) )
    self.gv_equip:setAutoRelocate(true);
    self.gv_equip:setPosition(200, 200);
    self.gv_equip:setColumns(4);
    self.gv_equip:setCountOfCell(16);
    self.gv_equip:setSizeOfCell(  cc.size(100,100) )
    cclook("你麻痹 --%s", 55)
    self.gv_equip:setLocalZOrder( 12 )
    self:addChild( self.gv_equip )
    cclook("你麻痹 --%s", 555)
    self.gv_equip:setDataSourceAdapterScriptHandler(function(pCell,idx)return self:onLoadCellOfEquipCell(pCell,idx)end)
    self.gv_equip:setCountOfCell(16)
    self.gv_equip:reloadData()
    cclook("你麻痹 --%s", 5555)
end


function RootLayer:onLoadCellOfEquipCell(p_cell,idx)
    cclook("你麻痹 --%s", idx)
    if p_cell==nil then 
        -- 创建cell
        local size = self.gv_equip:getSizeOfCell()
        p_cell = EquipViewCell:createForGvAndSize(self.gv_equip,size)
    end
    return p_cell
end


function EquipViewCell:createForGvAndSize(gv_father,size)
    local ret = nil
    ret = EquipViewCell:new()
    ret.size_cell = size
    ret.gv = gv_father
    ret.card = CCard:create()
    ret:addChild( ret.card )
    return ret
end

--]]
















------------------------------------------------------
--              公用的tips
------------------------------------------------------
function RootLayer:showTips_oneLine( s_text )
    local tips = cc.CSLoader:createNode("MainBattleLayer/Tips_oneLine.csb")   
    tips:setLocalZOrder( 100 )
    RootLayer.__instance:addChild( tips )
    local lab = tips:getChildByName("txt")
    if s_text then 
        lab:setString( s_text )
    end
    ----
    local function endFunc()
        tips:removeFromParent()
    end
    local evt = cc.CallFunc:create(endFunc) 
    local del = cc.DelayTime:create( 1.5 )
    local mov = cc.MoveBy:create( 0.5, cc.p(0,50) )
    local fade = cc.FadeOut:create( 0.5 )
    local spawn = cc.Spawn:create(mov, fade)

    local seq = cc.Sequence:create( del, spawn, evt )
    tips:runAction( seq )
end





function RootLayer:showTips_cut()
    -- body
end











---------------------------------------------------------------->
function take_1(scene)

    cc.SpriteFrameCache:getInstance():addSpriteFrames("TestMainMenu/img/img.plist")

    local node = cc.CSLoader:createNode("TestMainMenu/MainMenu.csb")     -- ("res/studio/login.csb")   -- ("res/test_a/BattleLayer.csb")
    scene:addChild( node )

    local node_2 = cc.CSLoader:createNode("TestMainMenu/MainMenu.csb")     -- ("res/studio/login.csb")   -- ("res/test_a/BattleLayer.csb")
    scene:addChild( node_2 )
    node_2:setPosition( cc.p(230, 0))
    node_2:setOpacity(90) 
    -- cc.Director:getInstance():getRunningScene():addChild( node )
    cclook("  -----\n\n end \n\n------ ")



    local function click(p)
        cclook(" 大傻逼 %s", p )
        local ran = math.random(5,20)
        p:setScale( 0.1*ran )
    end
    local function move(event)
        if  event.name == "began" then
            cclook("我在开始")
            return true
        elseif  event.name == "move" then
            cclook("我在移动")
            return true
        end
    end


    local btn_1 = node:getChildByName("BottomBar") :getChildByName("Pc_Bg_4") :getChildByName("BtnAltar_4") 
    btn_1:setVisible(false)
    btn_1:addClickEventListener(click)


    local btn = node_2:getChildByName("BottomBar") :getChildByName("Pc_Bg_4") :getChildByName("BtnAltar_4") 
    btn:addClickEventListener(click)

end



function take_2(scene)
    local node = cc.CSLoader:createNode("TestSelect/Layer.csb")     -- ("res/studio/login.csb")   -- ("res/test_a/BattleLayer.csb")
    scene:addChild( node )



    local nod2 = cc.CSLoader:createNode("TestSelect/Cell_2.csb")
    -- scene:addChild( nod2 )
    -- nod2:setPosition( cc.p(115,90) )    
    --node_2:setPosition( cc.p(230, 0))
    --node_2:setOpacity(90) 
    -- cc.Director:getInstance():getRunningScene():addChild( node )

    local lv = node:getChildByName("ListView_1")
    lv:setPosition( cc.p(200, 100))

    --lv:setItemModel(nod2)
    lv:pushBackDefaultItem()
    --lv:pushBackDefaultItem()
    -- lv:pushBackCustomItem( nod2 )

    local count = lv:getItems()
    cclook( " 获取到 getItems 是啥?  %s", count )
    cclook("  -----\n\n end \n\n------ ")
end
















